import {
  validateStrByte,
  validateCustomPositiveInt,
  validateCustomTwoDecimal
} from '@/utils/validate.js'
import {
  isNull
} from '@/utils'

export default {
  data() {
    // 校验方法
    // 券名
    const validateCardName = (rule, value, callback) => {
      if (isNull(value)) {
        callback(new Error('请输入卡名称'))
      } else if (!validateStrByte(value, 27)) {
        callback(new Error('长度不超过9个汉字或27个英文数字'))
      } else {
        callback()
      }
    }

    // 时间
    const validateTimes = (rule, value, callback) => {
      if (isNull(value)) {
        callback(new Error('请输入次数'))
      } else if (!validateCustomPositiveInt(value, 6, [1000000])) {
        callback(new Error('请输入正整数，范围1~1000000'))
      } else {
        callback()
      }
    }
    // 面额
    const validateDenomination = (rule, value, callback) => {
      if (isNull(value)) {
        callback(new Error('请输入面额'))
      } else if (!validateCustomTwoDecimal(value, 6)) {
        callback(new Error('请输入0.01~999999.99'))
      } else {
        callback()
      }
    }

    // 时间
    const validateDate = (rule, value, callback) => {
      if (!value || value.length !== 2) {
        callback(new Error('请选择日期'))
      } if (value[0] === value[1]) {
        callback(new Error('开始日期不能等于结束日期'))
      } else {
        if (this.form.availableTime.timeType === 4) this.$refs.form.validateField(['availableTime.availableDateList'])
        callback()
      }
    }

    // 时间类型一 多少天可用
    const validateDays = (rule, value, callback) => {
      if (isNull(value)) {
        callback(new Error('请输入天数'))
      } else if (!validateCustomPositiveInt(value, 3)) {
        callback(new Error('请输入1~999'))
      } else {
        callback()
      }
    }

    // 时间类型四 可用日期段
    const validateAvailableDateList = (rule, value, callback) => {
      const startDateArr = []
      const endDateArr = []
      value.some(v => {
        if (!v.date || v.date.length !== 2) {
          callback(new Error('请输入日期区间'))
          return true
        } else if (this.form.date && this.form.date.length === 2 && (v.date[0] + ' 00:00:00' < this.form.date[0] || v.date[1] + ' 23:59:59' > this.form.date[1])) {
          callback(new Error('日期区间不在可用时间内'))
          return true
        }
        if (value.length === 1) {
          callback()
          return true
        } else {
          startDateArr.push(v.date[0])
          endDateArr.push(v.date[1])
        }
      })
      // 排序
      const startDateSortArr = startDateArr.sort()
      const endDateSortArr = endDateArr.sort()
      // 判断交叉重复
      let flag = false
      for (var k = 1; k < startDateSortArr.length; k++) {
        if (startDateSortArr[k] <= endDateSortArr[k - 1]) {
          flag = true
        }
      }
      if (flag) {
        callback(new Error('日期区间存在交叉重叠'))
      } else {
        callback()
      }
    }

    //  时间类型二三四  可用时间段
    const validateAvailableTimeList = (rule, value, callback) => {
      const startTimeArr = []
      const endTimeArr = []
      value.some(v => {
        if (!v.time || v.time.length !== 2) {
          callback(new Error('请输入时间区间'))
          return true
        } else if (value.length === 1) {
          callback()
          return true
        } else {
          startTimeArr.push(v.time[0])
          endTimeArr.push(v.time[1])
        }
      })
      // 排序
      const startTimeSortArr = startTimeArr.sort()
      const endTimeSortArr = endTimeArr.sort()
      // 判断交叉重复
      let flag = false
      for (let k = 1; k < startTimeSortArr.length; k++) {
        if (startTimeSortArr[k] <= endTimeSortArr[k - 1]) {
          flag = true
        }
      }
      if (flag) {
        callback(new Error('时间区间存在交叉重叠'))
      } else {
        callback()
      }
    }

    //  用户天限制
    const validateMaxUseDay = (rule, value, callback) => {
      if (this.form.cardType === 'times') {
        if (!this.form.maxUseDayChecked) {
          callback()
        } else if (isNull(value)) {
          callback(new Error('请输入限制'))
        } else if (!validateCustomPositiveInt(value, 6, [1000000])) {
          callback(new Error('请输入正整数，范围1~1000000'))
        } else if (this.form.times && validateCustomPositiveInt(value, 6, [1000000]) && +value > +this.form.times) {
          callback(new Error('日限制要小于等于总次数'))
        } else {
          this.$refs.form.validateField(['maxUseWeek', 'maxUseMonth'])
          callback()
        }
      } else if (this.form.cardType === 'gift') {
        if (!this.form.maxUseDayChecked) {
          callback()
        } else if (isNull(value)) {
          callback(new Error('请输入限制'))
        } else if (this.form.denomination && validateCustomTwoDecimal(value, 6) && +value > +this.form.denomination) {
          callback(new Error('日限制要小于等于总面额'))
        } else if (!validateCustomTwoDecimal(value, 6)) {
          callback(new Error('请输入0.01~999999.99'))
        } else {
          this.$refs.form.validateField(['maxUseWeek', 'maxUseMonth'])
          callback()
        }
      } else {
        callback()
      }
    }

    //  用户周限制
    const validateMaxUseWeek = (rule, value, callback) => {
      if (this.form.cardType === 'times') {
        if (!this.form.maxUseWeekChecked) {
          callback()
        } else if (isNull(value)) {
          callback(new Error('请输入限制'))
        } else if (!validateCustomPositiveInt(value, 6, [1000000])) {
          callback(new Error('请输入正整数，范围1~1000000'))
        } else if (this.form.times && validateCustomPositiveInt(value, 6, [1000000]) && +value > +this.form.times) {
          callback(new Error('周限制要小于等于总次数'))
        } else if (this.form.maxUseDayChecked && this.form.maxUseDay && validateCustomPositiveInt(this.form.maxUseDay, 10) && +this.form.maxUseDay > +value) {
          callback(new Error('周限制要大于等于日限制'))
        } else {
          this.$refs.form.validateField(['maxUseMonth'])
          callback()
        }
      } else if (this.form.cardType === 'gift') {
        if (!this.form.maxUseWeekChecked) {
          callback()
        } else if (isNull(value)) {
          callback(new Error('请输入限制'))
        } else if (!validateCustomTwoDecimal(value, 6)) {
          callback(new Error('请输入0.01~999999.99'))
        } else if (this.form.denomination && validateCustomTwoDecimal(value, 6) && +value > +this.form.denomination) {
          callback(new Error('周限制要小于等于总面额'))
        } else if (this.form.maxUseDayChecked && this.form.maxUseDay && validateCustomTwoDecimal(this.form.maxUseDay) && +this.form.maxUseDay > +value) {
          callback(new Error('周限制要大于等于日限制'))
        } else {
          this.$refs.form.validateField(['maxUseMonth'])
          callback()
        }
      } else {
        callback()
      }
    }

    // 用户月限制
    const validateMaxUseMonth = (rule, value, callback) => {
      if (this.form.cardType === 'times') {
        if (!this.form.maxUseMonthChecked) {
          callback()
        } else if (isNull(value)) {
          callback(new Error('请输入限制'))
        } else if (!validateCustomPositiveInt(value, 6, [1000000])) {
          callback(new Error('请输入正整数，范围1~1000000'))
        } else if (this.form.times && validateCustomPositiveInt(value, 6, [1000000]) && +value > +this.form.times) {
          callback(new Error('周限制要小于等于总次数'))
        } else if (this.form.maxUseWeekChecked && this.form.maxUseWeek && validateCustomPositiveInt(this.form.maxUseWeek, 10) && +this.form.maxUseWeek > +value) {
          callback(new Error('月限制要大于等于周限制'))
        } else if (this.form.maxUseDayChecked && this.form.maxUseDay && validateCustomPositiveInt(this.form.maxUseDay, 10) && +this.form.maxUseDay > +value) {
          callback(new Error('月限制要大于等于日限制'))
        } else {
          callback()
        }
      } else if (this.form.cardType === 'gift') {
        if (!this.form.maxUseMonthChecked) {
          callback()
        } else if (isNull(value)) {
          callback(new Error('请输入限制'))
        } else if (!validateCustomTwoDecimal(value, 6)) {
          callback(new Error('请输入0.01~999999.99'))
        } else if (this.form.denomination && validateCustomTwoDecimal(value, 6) && +value > +this.form.denomination) {
          callback(new Error('月限制要小于等于总面额'))
        } else if (this.form.maxUseWeekChecked && this.form.maxUseWeek && validateCustomTwoDecimal(this.form.maxUseWeek) && +this.form.maxUseWeek > +value) {
          callback(new Error('月限制要大于等于周限制'))
        } else if (this.form.maxUseDayChecked && this.form.maxUseDay && validateCustomTwoDecimal(this.form.maxUseDay) && +this.form.maxUseDay > +value) {
          callback(new Error('月限制要大于等于日限制'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }

    // 核券
    const validateVerifyMerchant = (rule, value, callback) => {
      if (this.form.verifyMerchant !== 3) {
        this.$refs.form.clearValidate(['verifyMerchantList'])
        callback()
      } else {
        callback()
      }
    }
    const validateVerifyMerchantList = (rule, value, callback) => {
      if (this.form.verifyMerchant === 3 && value.length === 0) {
        callback(new Error('请选择商户'))
      } else {
        callback()
      }
    }

    //  核销业务类型
    const validateVerifyBusiness = (rule, value, callback) => {
      if (this.form.verifyBusinessType === 2 && value.length === 0) {
        callback(new Error('请选择类型'))
      } else {
        callback()
      }
    }

    return {
      // 校验字段
      rules: {
        // 券名
        cardName: [
          {
            required: true,
            validator: validateCardName,
            trigger: ['blur', 'change']
          }
        ],
        // 面额
        times: [
          {
            required: true,
            validator: validateTimes,
            trigger: ['blur', 'change']
          }
        ],
        // 面额
        denomination: [
          {
            required: true,
            validator: validateDenomination,
            trigger: ['blur', 'change']
          }
        ],
        // 可用日期
        date: [
          {
            required: true,
            validator: validateDate,
            trigger: ['blur', 'change']
          }
        ],
        // 时间类型一 多少天可用
        'availableTime.days': [
          {
            required: true,
            validator: validateDays,
            trigger: ['blur', 'change']
          }
        ],
        // 时间类型二 周几可用
        'availableTime.availablePeriodWeek': [
          {
            type: 'array',
            required: true,
            message: '请选择日期',
            trigger: ['blur', 'change']
          }
        ],
        // 时间类型三 几号可用
        'availableTime.availablePeriodDay': [
          {
            type: 'array',
            required: true,
            message: '请选择日期',
            trigger: ['blur', 'change']
          }
        ],
        // 时间类型四 可用日期段
        'availableTime.availableDateList': [
          {
            required: true,
            validator: validateAvailableDateList,
            trigger: ['blur', 'change']
          }
        ],
        // 时间类型二三四 可用时间段
        'availableTime.availableTimeList': [
          {
            required: true,
            validator: validateAvailableTimeList,
            trigger: ['blur', 'change']
          }
        ],
        // 用户日预算
        maxUseDay: [
          {
            required: true,
            validator: validateMaxUseDay,
            trigger: ['blur', 'change']
          }
        ],
        // 用户周预算
        maxUseWeek: [
          {
            required: true,
            validator: validateMaxUseWeek,
            trigger: ['blur', 'change']
          }
        ],
        // 用户月预算
        maxUseMonth: [
          {
            required: true,
            validator: validateMaxUseMonth,
            trigger: ['blur', 'change']
          }
        ],
        // 核券
        verifyMerchant: [
          {
            required: true,
            validator: validateVerifyMerchant,
            trigger: ['blur', 'change']
          }
        ],
        // 核券
        verifyMerchantList: [
          {
            type: 'array',
            required: true,
            validator: validateVerifyMerchantList,
            trigger: ['blur', 'change']
          }
        ],
        // 核销业务
        verifyBusiness: [
          {
            type: 'array',
            required: true,
            validator: validateVerifyBusiness,
            trigger: ['blur', 'change']
          }
        ],
        // 背景主题
        theme: [
          {
            required: true,
            message: '请选择背景主题',
            trigger: ['blur', 'change']
          }
        ],
        // 背景主题
        detailPage: [
          {
            required: true,
            message: '请上传图片',
            trigger: ['blur', 'change']
          }
        ],
        // 按钮文案
        'functionButtonConfig.word': [
          {
            required: true,
            message: '请输入按钮文案',
            trigger: ['blur', 'change']
          }
        ],
        // 小程序APPID
        'functionButtonConfig.appId': [
          {
            required: true,
            message: '请输入小程序APPID',
            trigger: ['blur', 'change']
          }
        ]

      }
      // 配置
      // 状态
      // 数据
    }
  },
  watch: {
    'form.verifyBusinessType'(newVal) {
      if (newVal === 2 && this.form.verifyBusiness && this.form.verifyBusiness.length === 0) {
        this.form.verifyBusiness = ['9000', '9056']
      }
    }
  },
  computed: {},
  methods: {}
}
